package com.jch.dm1_immutable_object.demo;

import java.util.Random;

public class TestClient {

    public static void main(String[] args) throws InterruptedException {
        TestImmutableObject s0 = TestManipulator.getInstance().getStateByKey("key0");
        System.out.println("Thread: main,s0:" + s0.toString());
        Thread changeStateThread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Random random = new Random();
                    int i = random.nextInt(3);
                    System.out.println("Thread: " + Thread.currentThread().getName());
                    TestManipulator.getInstance().changeStateTo("key" + i, new TestImmutableObject("s-00", 0));
                }
            }
        });
        changeStateThread.setName("changeStateThread");
        changeStateThread.start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("Thread:" + Thread.currentThread().getName());
                    TestManipulator testManipulator = new TestManipulator();
                    testManipulator.setInstance(testManipulator);
                }
            }
        },"resetStateGroup").start();
        while (true){
            TestImmutableObject s = TestManipulator.getInstance().getStateByKey("key0");
            System.out.println("Thread: main,s0:" + s.toString());
            Thread.sleep(500);
        }
    }

}
